﻿/*
 * Форма для актора визуализатора ЛАЧХ
 * */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ctem.samples.actors.concurrent
{
        public partial class BodeForm : Form
        {
            public delegate void OnDataAppearingHandler(double[] amp,double[] phs);
            OnDataAppearingHandler oda;
            public void SetArrayOfData(double[] amp, double[] phs)
            {
                this.Invoke(oda, amp, phs);
            }

            private void TopMostToolStripMenuItem_Click(object sender, EventArgs e)
            {
                this.TopMost = !this.TopMost;
            }

            private void выполнитьToolStripMenuItem_Click(object sender, EventArgs e)
            {
                bool stability = false;
                float LA4X_X_Y0 = signalChartBox1.GetSeries("ЛАЧХ").GetItem(0).X;
                List<float> LF4X_X_Y_180 = new List<float>();
                for (int i = 0; i < signalChartBox1.GetSeries("ЛАЧХ").CountOfPoints; i++)
                {
                    if (signalChartBox1.GetSeries("ЛАЧХ").GetItem(i).Y <= 0)
                    {
                        LA4X_X_Y0 = signalChartBox1.GetSeries("ЛАЧХ").GetItem(i).X;
                        break;
                    }
                    if (signalChartBox2.GetSeries("ЛФЧХ").GetItem(i).Y == -180)
                        LF4X_X_Y_180.Add(signalChartBox2.GetSeries("ЛФЧХ").GetItem(i).X);
                }
                if (LF4X_X_Y_180.Count % 2 == 0) stability = true; else stability = false;
                if (stability) MessageBox.Show("Система устойчива"); else MessageBox.Show("Система не устойчива");

            }

             public void ReInitialize()
            {
                signalChartBox1.Clear();
                signalChartBox2.Clear();
            }

            public BodeForm(int N)
            {
                oda = new OnDataAppearingHandler(PrintBode);
                InitializeComponent();
                //ЛАЧХ
                signalChartBox1.IsLog = true;
                signalChartBox1.CollapsSignal(true);
                signalChartBox1.SetAxisCaption('x', "Частота", "1/сек");
                signalChartBox1.SetAxisCaption('y', "Амплитуда", "дБ");
                //ЛФЧХ
                signalChartBox2.IsLog = true;
                signalChartBox2.CollapsSignal(true);
                signalChartBox2.SetAxisCaption('x', "Частота", "1/сек");
                signalChartBox2.SetAxisCaption('y', "Фаза", "град");

                //RealSetMagicNumberNInterval(N);
                signalChartBox1.AddSeries(new ctem.gui.SignalChart.Series("ЛАЧХ", Color.Red));
                signalChartBox2.AddSeries(new ctem.gui.SignalChart.Series("ЛФЧХ", Color.Blue));
            }

            private void PrintBode(double[] BodeAmplitude,double[] BodePhase)
            {
                for (int i = 1; i < BodeAmplitude.Length; i++)
                {
                    signalChartBox1.AddItem("ЛАЧХ", new PointF(i, (float)BodeAmplitude[i]));
                    signalChartBox2.AddItem("ЛФЧХ", new PointF(i, (float)BodePhase[i]));
                }

            }


        }


}